package order.consumer;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerOrderly;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

/**
 * order包 ，顺序消息实例
 * OrderMsgConsumer 顺序消息消费者
 * 1个线程消费一个queue,保证消息被顺序消费。
 * @author: BacHe
 * @email: 1218585258@qq.com
 * @Date: 2020/12/8 23:27
 */
public class OrderMsgConsumer {
    public static void main(String[] args) throws MQClientException {
        //1.创建消费者consumer，指定消费者组
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("group4");
        //2.指定nameserver地址
        consumer.setNamesrvAddr("192.168.0.88:9876");
        //3.订阅主题Topic 和 tag  （tag 可以用 || 表达式 ，也可以用 * ）
        consumer.subscribe("order","tag4");


        //TODO 4.注册消息监听器,设置回调函数，处理消息.
        //TODO MessageListenerOrderly 消费顺序消息。
        //TODO MessageListenerConcurrently，并发的消息监听器，Concurrently 并发的
        consumer.registerMessageListener(new MessageListenerOrderly() {
            @Override
            public ConsumeOrderlyStatus consumeMessage(List<MessageExt> list, ConsumeOrderlyContext consumeOrderlyContext) {
                list.forEach(messageExt -> System.out.println("线程名称:"+Thread.currentThread().getName()+","+new String(messageExt.getBody())));
                return ConsumeOrderlyStatus.SUCCESS;
            }
        });
        //5.启动消费者consumer
        consumer.start();
        //观察到结果：1个线程去消费同一个queue，保证同一个orderId的4个业务，被顺序消费。

//        线程名称:ConsumeMessageThread_2,OrderStep{orderId=5, desc='1.订单创建', order=1}
//        线程名称:ConsumeMessageThread_1,OrderStep{orderId=4, desc='1.订单创建', order=1}
//        线程名称:ConsumeMessageThread_2,OrderStep{orderId=1, desc='1.订单创建', order=1}
//        线程名称:ConsumeMessageThread_2,OrderStep{orderId=1, desc='2.订单付款', order=2}
//        线程名称:ConsumeMessageThread_2,OrderStep{orderId=5, desc='2.订单付款', order=2}
//        线程名称:ConsumeMessageThread_1,OrderStep{orderId=4, desc='2.订单付款', order=2}
//        线程名称:ConsumeMessageThread_2,OrderStep{orderId=1, desc='3.订单推送', order=3}
//        线程名称:ConsumeMessageThread_2,OrderStep{orderId=5, desc='3.订单推送', order=3}
//        线程名称:ConsumeMessageThread_2,OrderStep{orderId=5, desc='4.订单完成', order=4}
//        线程名称:ConsumeMessageThread_2,OrderStep{orderId=1, desc='4.订单完成', order=4}
//        线程名称:ConsumeMessageThread_1,OrderStep{orderId=4, desc='3.订单推送', order=3}
//        线程名称:ConsumeMessageThread_1,OrderStep{orderId=4, desc='4.订单完成', order=4}
//        线程名称:ConsumeMessageThread_3,OrderStep{orderId=2, desc='1.订单创建', order=1}
//        线程名称:ConsumeMessageThread_3,OrderStep{orderId=2, desc='2.订单付款', order=2}
//        线程名称:ConsumeMessageThread_3,OrderStep{orderId=2, desc='3.订单推送', order=3}
//        线程名称:ConsumeMessageThread_3,OrderStep{orderId=2, desc='4.订单完成', order=4}
//        线程名称:ConsumeMessageThread_4,OrderStep{orderId=3, desc='1.订单创建', order=1}
//        线程名称:ConsumeMessageThread_4,OrderStep{orderId=3, desc='2.订单付款', order=2}
//        线程名称:ConsumeMessageThread_4,OrderStep{orderId=3, desc='3.订单推送', order=3}
//        线程名称:ConsumeMessageThread_4,OrderStep{orderId=3, desc='4.订单完成', order=4}



    }
}
